<template>
  <el-aside width="240px" class="app-sidebar">
    <div class="sidebar-header">
      <div class="user-info">
        <el-avatar :size="60" :src="defaultAvatar" />
        <div class="user-details">
          <div class="user-name">{{ userName }}</div>
          <div class="user-id">ID: {{ userId }}</div>
          <div class="user-role">{{ userRoleText }}</div>
        </div>
      </div>
    </div>
    
    <el-menu
      :default-active="activeMenu"
      class="sidebar-menu"
      router
      background-color="#304156"
      text-color="#bfcbd9"
      active-text-color="#409EFF"
    >
      <!-- 教师系统菜单 -->
      <template v-if="userType === 'teacher'">
        <el-menu-item index="/class">
          <el-icon><HomeFilled /></el-icon>
          <span>班级列表</span>
        </el-menu-item>
        
        <el-sub-menu index="class-management">
          <template #title>
            <el-icon><Menu /></el-icon>
            <span>班级管理</span>
          </template>
          <el-menu-item :index="`/teacher-class/list/`">班级信息</el-menu-item>
          <el-menu-item :index="`/teacher-class/detail/students`">学生管理</el-menu-item>
        </el-sub-menu>
        
        <el-sub-menu index="course-management">
          <template #title>
            <el-icon><Notebook /></el-icon>
            <span>课程管理</span>
          </template>
          <el-menu-item index="/course-list">课程列表</el-menu-item>
          <el-menu-item index="/my-courses">我的课程</el-menu-item>
        </el-sub-menu>
        
        <el-menu-item index="/practice">
          <el-icon><Calendar /></el-icon>
          <span>实践活动</span>
        </el-menu-item>
        
        <!-- 修正后的咨询管理菜单 -->
  <el-sub-menu index="question-management">
    <template #title>
      <el-icon><Document /></el-icon>
      <span>咨询管理</span>
    </template>
    <el-menu-item index="/questions">咨询列表</el-menu-item>
    <el-menu-item index="/my-consultation">我的咨询</el-menu-item>
  </el-sub-menu>
      </template>

      <!-- 学生系统菜单 -->
      <template v-else-if="userType === 'student'">
        <el-menu-item index="/student-courses">
          <el-icon><Notebook /></el-icon>
          <span>我的课程</span>
        </el-menu-item>
        
        <el-menu-item index="/course-list">
          <el-icon><List /></el-icon>
          <span>全部课程</span>
        </el-menu-item>
        
        <el-menu-item index="/classes">
          <el-icon><School /></el-icon>
          <span>我的班级</span>
        </el-menu-item>
        
<el-sub-menu index="course-selection">
  <template #title>
    <el-icon><ShoppingCart /></el-icon>
    <span>选课购物</span>
  </template>
  <!-- 确保index与路由path完全一致 -->
  <el-menu-item index="/student/shopcart">购物车</el-menu-item>
  <el-menu-item index="/student/selectCourse">选课中心</el-menu-item>
</el-sub-menu>
        
        <el-menu-item index="/student/progress">
          <el-icon><DataLine /></el-icon>
          <span>学习进度</span>
        </el-menu-item>
        
        <el-sub-menu index="practice-activities">
          <template #title>
            <el-icon><Calendar /></el-icon>
            <span>实践活动</span>
          </template>
          <el-menu-item index="/student/practice/list">实践列表</el-menu-item>
          <el-menu-item index="/student/practice/registered">已报名实践</el-menu-item>
        </el-sub-menu>
        
        <!-- 修正后的咨询管理菜单 -->
  <el-sub-menu index="question-management">
    <template #title>
      <el-icon><Document /></el-icon>
      <span>咨询管理</span>
    </template>
    <el-menu-item index="/questions">咨询列表</el-menu-item>
    <el-menu-item index="/my-consultation">我的咨询</el-menu-item>
  </el-sub-menu>
      </template>
    </el-menu>
  </el-aside>
</template>

<script setup>
import { computed, onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import defaultAvatar from '@/assets/logo.png'
import {
  HomeFilled,
  Menu,
  Notebook,
  Calendar,
  Document,
  List,
  School,
  ShoppingCart,
  DataLine,
  ChatLineRound,
  Trophy
} from '@element-plus/icons-vue'

const route = useRoute()
const authStore = useAuthStore()
const loading = ref(false)
const error = ref(null)

const activeMenu = computed(() => route.path)
const userType = computed(() => authStore.userType || '')
const userName = computed(() => authStore.userInfo?.name || authStore.userInfo?.username || '未知用户')
const userId = computed(() => authStore.userInfo?.id || authStore.userInfo?.userId || '未知ID')
const userAvatar = computed(() => {
  // 处理头像路径，确保是完整URL或使用默认头像
  const avatar = authStore.userInfo?.avatar || authStore.userInfo?.pic
  return avatar ? (avatar.startsWith('http') ? avatar : `/api/uploads/${avatar}`) : defaultAvatar
})

const userRoleText = computed(() => {
  const roleMap = {
    teacher: '教师',
    student: '学生',
    admin: '管理员',
    0: '教师', // 兼容数字类型的角色
    1: '学生',
    2: '管理员'
  }
  return roleMap[authStore.userInfo?.role] || roleMap[userType.value] || '未知角色'
})

// 初始化时检查用户信息
onMounted(() => {
  if (!authStore.isAuthenticated) {
    error.value = '用户未登录，请先登录'
  }
})
</script>

<style scoped>
.app-sidebar {
  height: 100vh;
  background-color: #304156;
  transition: width 0.3s;
}

.sidebar-header {
  padding: 20px 10px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
  color: #fff;
}

.user-details {
  flex: 1;
}

.user-name {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-id {
  font-size: 12px;
  color: #a8abb2;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role {
  font-size: 12px;
  color: #8a8b90;
  padding: 2px 6px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  display: inline-block;
}

.sidebar-menu {
  border-right: none;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 240px;
}

.el-menu-item.is-active {
  background-color: #263445 !important;
}

.el-sub-menu .el-menu-item {
  min-width: 240px !important;
}

.error-message {
  color: #f56c6c;
  padding: 10px;
  font-size: 14px;
}
</style>